package com.example.producer.producer;

import com.example.producer.dto.UserDto;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ProducerApp {

    private static String USER_EXCHANGE = "userExchange";

    private static String USER_QUEUE = "user-queue";

    private static String ROUTING_KEY = "user.key";

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void sendMsg() throws Exception{
        ObjectMapper mapper = new ObjectMapper();

        UserDto dto = new UserDto();
        dto.setSex("男");
        dto.setUserName("张三");
//        rabbitTemplate.setConfirmCallback(confimCallback);
        // 消息唯一ID
        CorrelationData correlationData = new CorrelationData(dto.getId());

        rabbitTemplate.convertAndSend(USER_EXCHANGE,ROUTING_KEY,mapper.writeValueAsString(dto),correlationData);
    }
}
